package com.atguigu.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

public class SqlSessionUtils {
    private static SqlSessionFactory sqlSessionFactory;

    static {
        InputStream is;
        try {
            is = Resources.getResourceAsStream("mybatis-config.xml");

        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
    }
    //不开启自动提交
    public static SqlSession openSession(){
        return sqlSessionFactory.openSession();
    }
    //开启自动提交
    public static SqlSession openAutoCommitSession(){
        return sqlSessionFactory.openSession(true);
    }

}
